package cn.ds.assets.module.assets.service.metadataModelManage;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.metadataModelManage.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataModelManage.BaseMetadataModelManageDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.metadataModelManage.BaseMetadataModelManageConvert;
import cn.ds.assets.module.assets.dal.mysql.metadataModelManage.BaseMetadataModelManageMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 元数据管理-元模型-管理属性 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseMetadataModelManageServiceImpl implements BaseMetadataModelManageService {

    @Resource
    private BaseMetadataModelManageMapper baseMetadataModelManageMapper;

    @Override
    public Long createBaseMetadataModelManage(BaseMetadataModelManageCreateReqVO createReqVO) {
        // 插入
        BaseMetadataModelManageDO baseMetadataModelManage = BaseMetadataModelManageConvert.INSTANCE.convert(createReqVO);
        baseMetadataModelManageMapper.insert(baseMetadataModelManage);
        // 返回
        return baseMetadataModelManage.getId();
    }

    @Override
    public void updateBaseMetadataModelManage(BaseMetadataModelManageUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseMetadataModelManageExists(updateReqVO.getId());
        // 更新
        BaseMetadataModelManageDO updateObj = BaseMetadataModelManageConvert.INSTANCE.convert(updateReqVO);
        baseMetadataModelManageMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseMetadataModelManage(Long id) {
        // 校验是否存在
        validateBaseMetadataModelManageExists(id);
        // 逻辑删除
        baseMetadataModelManageMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseMetadataModelManageExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseMetadataModelManageDO dataDo = baseMetadataModelManageMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseMetadataModelManageDO getBaseMetadataModelManage(Long id) {
        return baseMetadataModelManageMapper.selectById(id);
    }

    @Override
    public List<BaseMetadataModelManageDO> getBaseMetadataModelManageList(Collection<Long> ids) {
        return baseMetadataModelManageMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseMetadataModelManageDO> getBaseMetadataModelManagePage(BaseMetadataModelManagePageReqVO pageReqVO) {
        return baseMetadataModelManageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseMetadataModelManageDO> getBaseMetadataModelManageList(BaseMetadataModelManageExportReqVO exportReqVO) {
        return baseMetadataModelManageMapper.selectList(exportReqVO);
    }

}
